﻿using System;
using System.Collections.ObjectModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_searchresult_default_list_sub_thread : BoardSearchSubPartBase
{
	private Board CurrentBoard;
	private BoardThread CurrentThread;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			ThreadListRepeater.DataSource = BoardThread.GetSearchingThreadCollection(
			Server.UrlDecode(BoardParameters.SearchText), PageSize, PageSize * BoardParameters.PageIndex);
			ThreadListRepeater.DataBind();
		}
	}

	protected virtual void ThreadListRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			CurrentThread = (BoardThread)e.Item.DataItem;

			CurrentBoard = Board.GetBoard(CurrentThread.ComponentID);

			#region 게시판 이름
			((Literal)e.Item.FindControl("BoardNameLiteral")).Text = String.Format(GetLocalResourceObject("BoardFormat").ToString(), CurrentBoard.Name);
			#endregion

			#region 강조
			Image StressImage = (Image)e.Item.FindControl("StressImage");
			StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_Normal_ImageUrl").ToString();
			StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_Normal_Text").ToString();

			if (BoardParameters.ThreadID == CurrentThread.ThreadID)
			{
				//현재글
				StressImage.ImageUrl = GetLocalResourceObject("Symbol_Current_ImageUrl").ToString();
				StressImage.AlternateText = GetLocalResourceObject("Symbol_Current_Text").ToString();
			}
			else if (CurrentThread.IsAnno)
			{
				//공지사항
				StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_Anno_ImageUrl").ToString();
				StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_Anno_Text").ToString();
			}
			else
			{
				//새글강조
				if (CurrentBoard.UseStressNew)
				{
					DateTime _now = DateTime.Now;
					TimeSpan _timeSpan = _now - CurrentThread.RegistrationDate;

					if (_timeSpan.TotalMinutes < 1440)
					{
						StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_New_ImageUrl").ToString();
						StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_New_Text").ToString();
					}
				}
				//오늘글강조
				if (CurrentBoard.UseStressToday)
				{
					if (DateTime.Now.ToShortDateString() == CurrentThread.RegistrationDate.ToShortDateString())
					{
						StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_Today_ImageUrl").ToString();
						StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_Today_Text").ToString();
					}
				}
				//인기글강조
				if (CurrentBoard.UseStressHot)
				{
					if (CurrentThread.ReadCount > CurrentBoard.HotCount)
					{
						StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_Hot_ImageUrl").ToString();
						StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_Hot_Text").ToString();
					}
				}
				//비밀글
				if (CurrentThread.IsSecret)
				{
					StressImage.ImageUrl = GetLocalResourceObject("Symbol_Stress_Secret_ImageUrl").ToString();
					StressImage.AlternateText = GetLocalResourceObject("Symbol_Stress_Secret_Text").ToString();
				}
			}
			#endregion

			if (CurrentUserInfo.IsAdministrator ||
				CurrentUserInfo.IsSysop(CurrentBoard) ||
				CurrentUserInfo.IsAuthorizedBy(CurrentBoard, BoardCommand.ListView))
			{
				#region 링크
				HyperLink threadHyperLink = (HyperLink)e.Item.FindControl("ThreadHyperLink");
				threadHyperLink.Text = CurrentThread.Title;

				if (!String.IsNullOrEmpty(CurrentThread.PageUrl))
				{
					threadHyperLink.NavigateUrl = CurrentThread.PageUrl;

					if (CurrentThread.IsSecret)
					{
						//작성자 검사
						//관리자 또는 시샵 또는 글작성자인지 확인
						if (!(CurrentUserInfo.IsAdministrator ||
							CurrentUserInfo.IsSysop(CurrentBoard) ||
							(CurrentThread.ProviderUserKey == CurrentUserInfo.ProviderUserKey)))
						{
							threadHyperLink.Enabled = false;
						}
					}

					if (!String.IsNullOrEmpty(BoardParameters.SearchText))
						threadHyperLink.Text = threadHyperLink.Text.Replace(BoardParameters.SearchText, String.Format(VUtility.GetVWebGlobalResource("General_SearchTextFormat"), BoardParameters.SearchText));
				}
				#endregion

				#region Category
				if (!string.IsNullOrEmpty(CurrentThread.CategoryName))
				{
					PlaceHolder CategoryPlaceHolder = (PlaceHolder)e.Item.FindControl("CategoryPlaceHolder");
					BoardCategory currentCategory = CurrentBoard.GetCategoryByID(CurrentThread.CategoryID);

					if (currentCategory.HasIconImage)
					{
						Image categoryImage = new Image();
						categoryImage.ImageUrl = currentCategory.IconImageUrl;
						categoryImage.AlternateText = currentCategory.Name;
						CategoryPlaceHolder.Controls.Add(categoryImage);
					}
					else
					{
						Label categoryNameLabel = new Label();
						categoryNameLabel.Text = String.Format(GetLocalResourceObject("CategoryFormat").ToString(), CurrentThread.CategoryName);
						CategoryPlaceHolder.Controls.Add(categoryNameLabel);
					}
				}
				#endregion

				#region 첨부파일, 링크
				PlaceHolder OptionIconPlaceHolder = (PlaceHolder)e.Item.FindControl("OptionIconPlaceHolder");
				Image optionImage = null;
				Literal empty = null;

				if (CurrentThread.Links != null)
				{
					optionImage = new Image();
					optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Link_ImageUrl").ToString();
					optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Link_Text").ToString();
					OptionIconPlaceHolder.Controls.Add(optionImage);
					empty = new Literal();
					empty.Text = "&nbsp;";
					OptionIconPlaceHolder.Controls.Add(empty);
				}

				if (CurrentThread.HasFile)
				{
					AttachmentFileInfoCollection attachmentFileInfoCollection = CurrentBoard.GetAttachmentFileManager().GetAttachmentFileInfoCollection(CurrentThread.ThreadID);

					if (attachmentFileInfoCollection != null)
					{
						Collection<String> contentTypes = new Collection<String>();

						foreach (AttachmentFileInfo fileInfo in attachmentFileInfoCollection)
						{
							if (fileInfo != null)
							{
								if (fileInfo.UploadFile.ContentType.IndexOf("image") > -1)
								{
									if (contentTypes.IndexOf("image") < 0)
										contentTypes.Add("image");
								}
								else if (fileInfo.UploadFile.ContentType.IndexOf("audio") > -1)
								{
									if (contentTypes.IndexOf("audio") < 0)
										contentTypes.Add("audio");
								}
								else if (fileInfo.UploadFile.ContentType.IndexOf("video") > -1)
								{
									if (contentTypes.IndexOf("video") < 0)
										contentTypes.Add("video");
								}
								else if (fileInfo.UploadFile.ContentType.IndexOf("flash") > -1)
								{
									if (contentTypes.IndexOf("flash") < 0)
										contentTypes.Add("flash");
								}
								else
								{
									if (contentTypes.IndexOf("etc") < 0)
										contentTypes.Add("etc");
								}
							}
						}

						foreach (String contentType in contentTypes)
						{
							switch (contentType)
							{
								case "image":
									optionImage = new Image();
									optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Image_ImageUrl").ToString();
									optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Image_Text").ToString();
									OptionIconPlaceHolder.Controls.Add(optionImage);
									empty = new Literal();
									empty.Text = "&nbsp;";
									OptionIconPlaceHolder.Controls.Add(empty);
									break;
								case "audio":
									optionImage = new Image();
									optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Audio_ImageUrl").ToString();
									optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Audio_Text").ToString();
									OptionIconPlaceHolder.Controls.Add(optionImage);
									empty = new Literal();
									empty.Text = "&nbsp;";
									OptionIconPlaceHolder.Controls.Add(empty);
									break;
								case "video":
									optionImage = new Image();
									optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Video_ImageUrl").ToString();
									optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Video_Text").ToString();
									OptionIconPlaceHolder.Controls.Add(optionImage);
									empty = new Literal();
									empty.Text = "&nbsp;";
									OptionIconPlaceHolder.Controls.Add(empty);
									break;
								case "flash":
									optionImage = new Image();
									optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Flash_ImageUrl").ToString();
									optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Flash_Text").ToString();
									OptionIconPlaceHolder.Controls.Add(optionImage);
									empty = new Literal();
									empty.Text = "&nbsp;";
									OptionIconPlaceHolder.Controls.Add(empty);
									break;
								case "etc":
									optionImage = new Image();
									optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_File_ImageUrl").ToString();
									optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_File_Text").ToString();
									OptionIconPlaceHolder.Controls.Add(optionImage);
									empty = new Literal();
									empty.Text = "&nbsp;";
									OptionIconPlaceHolder.Controls.Add(empty);
									break;
							}
						}
					}
				}
				#endregion
			}
			else
			{
				#region 링크
				HyperLink threadHyperLink = (HyperLink)e.Item.FindControl("ThreadHyperLink");
				threadHyperLink.Text = VUtility.GetVWebGlobalResource("Board_Do_ThreadList_Rejection");
				#endregion
			}

			if (CurrentUserInfo.IsAdministrator ||
				CurrentUserInfo.IsSysop(CurrentBoard) ||
				CurrentUserInfo.IsAuthorizedBy(CurrentBoard, BoardCommand.Read))
			{
				String content = CurrentThread.Content;

				if (!String.IsNullOrEmpty(content))
					content = VUtility.RemoveHtmlTag(content);

				if (CurrentThread.IsSecret)
				{
					//작성자 검사
					//관리자 또는 시샵 또는 글작성자인지 확인
					if (!(CurrentUserInfo.IsAdministrator ||
						CurrentUserInfo.IsSysop(CurrentBoard) ||
						(CurrentThread.ProviderUserKey == CurrentUserInfo.ProviderUserKey)))
					{
						content = String.Empty;
					}
				}

				if(content.Length > 100)
					content = content.Substring(0, 100).Trim() + GetLocalResourceObject("ReduceCharacter");

				((Literal)e.Item.FindControl("ContentLiteral")).Text = Server.HtmlEncode(content);
			}
			else
			{
				((Literal)e.Item.FindControl("ContentLiteral")).Text = VUtility.GetVWebGlobalResource("Board_Do_ThreadView_Rejection");
			}

			Literal urlLiteral = (Literal)e.Item.FindControl("UrlLiteral");

			if (CurrentThread.PageUrl.Length > 80)
				urlLiteral.Text = CurrentThread.PageUrl.Substring(0, 80).Replace("&", "&amp;");
			else
				urlLiteral.Text = CurrentThread.PageUrl.Replace("&", "&amp;");
		}
	}
}
